package org.jeecg.common.handler;

import cn.aresoft.excel.entity.ExcelDictModel;
import cn.aresoft.excel.handle.IDictListGetHandler;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class DictHandler implements IDictListGetHandler {

	private CommonAPI commonAPI = SpringContextUtils.getBean(CommonAPI.class);

	@Override
	public List<ExcelDictModel> getFieldDictList(String dictType, String dictTable, String code, String text) {
		List<ExcelDictModel> excelDictModelList = new ArrayList<>();
		List<DictModel> dictModelList = null;
		if(StringUtils.isNotEmpty(dictType) && StringUtils.isEmpty(dictTable)){
			dictModelList = commonAPI.queryDictItemsByCode(dictType);
		}else if(StringUtils.isNotEmpty(dictTable)){
			dictModelList = commonAPI.queryTableDictItemsByCode(dictTable,text,code);
		}
		if(CollectionUtils.isNotEmpty(dictModelList)){
			ExcelDictModel  excelDictModel = null;
			for(DictModel dictModel : dictModelList) {
				excelDictModel = new ExcelDictModel(dictModel.getValue(),dictModel.getText());
				excelDictModelList.add(excelDictModel);
			}
		}
		return excelDictModelList;
	}



}
